
layui.use(['form','layer', 'upload'], function () {

    var $ = layui.$, form = layui.form, layer=layui.layer, upload = layui.upload;

    $(function () {
        LoginStatus();

        var userInfo = JSON.parse($.cookie('userInfo'));
        $('#id').val(userInfo.id);
        $('#name').val(userInfo.name);
        $('#tel').val(userInfo.tel);
        $('#position').val(userInfo.position);
        $('#email').val(userInfo.email);
        $('#imageUrlUpload').attr('src', companyServices + userInfo.img);

    })



    //普通图片上传
    upload.render({
        elem: '#imageUrlUpload'
        , url: companyServices+"/user/upload/img"
        , before: function (obj) {
            //预读本地文件示例，不支持ie8

        }
        , done: function (res) {
            if(res.code==0){
                var fullPath = companyServices + res.data.src;
                $('#imageUrlUpload').attr('src',fullPath);
                $('#img').val(res.data.src) ;
                layer.msg(res.msg);
            }
        }
        , error: function () {
            layer.msg("上传失败");
        }
    });


    form.on('submit(btnSave)', function (data) {
        layer.load(2);
        var img = $('input[name="file"]').val()
        if(img == "" || img == null){
            layerMsg("图片未上传或正在上传中")
            return false;
        }
        $.ajax({
            type: 'post', // 提交方式 get/post
            url: companyServices+"/user/info", // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data:JSON.stringify(data.field),
            success: function(res) {
                layer.closeAll('loading');
                if(res.success){
                    data = res.data;
                    $.cookie('userInfo',JSON.stringify(data));
                    layerMsg(res.message);
                    setTimeout(function() {
                        window.location.href="wrap-company-set.html" ;
                    },2000);

                }else{
                    layerMsg(res.message)
                }
            },
        })

        return false;
    });



});




